package com.chess;

import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class ChessView extends View {

	private final int INTERVAL_DISTANCE = 30;
	private final int X_GRID_NUMBER = 20;
	private final int Y_GRID_NUMBER = 30;
	private ChessBoard chessBoard;
	private boolean isMyTurn = false;
	private int myColor;
	
	
	public ChessView(Context context, AttributeSet attrs) {
		super(context, attrs);
		chessBoard = new ChessBoard(X_GRID_NUMBER, Y_GRID_NUMBER);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawColor(Color.rgb(253,192,9));
		Paint paint = new Paint();
		paint.setColor(Color.RED);
		drawLines(canvas);
		drawPieces(canvas);
		
	}
	
	private void drawLines(Canvas canvas) {
		Paint paint = new Paint();
		paint.setColor(Color.BLACK);
		for (int i = 0; i < X_GRID_NUMBER; i++) {
			canvas.drawLine(i * INTERVAL_DISTANCE, 
					0, i * INTERVAL_DISTANCE, Y_GRID_NUMBER * INTERVAL_DISTANCE, 
					paint);
			
		}
		for (int i = 0; i < Y_GRID_NUMBER; i++) {
			canvas.drawLine(0, 
					i * INTERVAL_DISTANCE, 
					X_GRID_NUMBER * INTERVAL_DISTANCE, 
					i * INTERVAL_DISTANCE, 
					paint);
		}
	}
	
	private void drawPieces(Canvas canvas) {
		int opponentsColor = 0;
		if(myColor == Color.BLACK) opponentsColor = Color.WHITE;
		else opponentsColor = Color.BLACK;
		
		for (int i = 0; i < X_GRID_NUMBER; i++) {
			for (int j = 0; j < Y_GRID_NUMBER; j++) {
				if(chessBoard.occupiedByMe(i, j)) {
					Paint paint = new Paint();
					paint.setColor(myColor);
					drawPieceAtCrossPoint(canvas, paint, i, j);
				}
				else if(chessBoard.occupiedByOpponents(i, j)){
					Paint paint = new Paint();
					paint.setColor(opponentsColor);
					drawPieceAtCrossPoint(canvas, paint, i, j);
				}
			}
		}
	}
	
	private void drawPieceAtCrossPoint(Canvas canvas, Paint paint, int x, int y) {
		canvas.drawCircle(x * INTERVAL_DISTANCE, y * INTERVAL_DISTANCE, 
				12, paint);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(isMyTurn) {
			Point pointToDraw = getPointToDraw(event);
			if(chessBoard.addPieceByMe(pointToDraw.x, pointToDraw.y)) {
				invalidate();
				Intent intent = new Intent();
				intent.setAction("com.chess.SEND_POSITION");
				intent.putExtra("positionMsg", pointToDraw.x + "-" + pointToDraw.y);
				if(chessBoard.win(pointToDraw.x, pointToDraw.y)) {
					intent.putExtra("over", "win");
				}
				getContext().sendBroadcast(intent);
				isMyTurn = false;
			}
		}
		return super.onTouchEvent(event);
	}
	
	private Point getPointToDraw(MotionEvent event) {
		
		Point touchPoint = new Point((int)event.getX(), (int)event.getY());
		int x = (int)(event.getX() / INTERVAL_DISTANCE);
		int y = (int)(event.getY() / INTERVAL_DISTANCE);
		
		Point[] fourNearPoints = {
			new Point(x , y),
			new Point(x + 1, y),
			new Point(x, y + 1),
			new Point(x + 1, y + 1)
		};
		return getMinDistancePoint(touchPoint, fourNearPoints);
	}

	private Point getMinDistancePoint(Point touchPoint, Point[] fourNearPoints) {
		
		int indexToFind = 0;
		int minDistance = 1000;
		for (int i = 0; i < fourNearPoints.length; i++) {
			Point nearPointPosition = new Point(fourNearPoints[i].x * INTERVAL_DISTANCE,
					fourNearPoints[i].y * INTERVAL_DISTANCE);
			int distance = getDistanceBetweentTwoPoint(touchPoint, nearPointPosition);
			if(distance < minDistance) {
				indexToFind = i;
				minDistance = distance;
			}
		}
		return fourNearPoints[indexToFind];
	}

	private int getDistanceBetweentTwoPoint(Point p1, Point p2) {
		return (p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) * (p1.y - p2.y);
	}
	
	public void addPieceByOpponents(int x, int y) {
		chessBoard.addPieceByOpponents(x, y);
		invalidate();
	}

	public boolean isMyTurn() {
		return isMyTurn;
	}

	public void setMyTurn(boolean isMyTurn) {
		this.isMyTurn = isMyTurn;
	}

	public void setMyColor(int myColor) {
		this.myColor = myColor;
	}
	
	public void clear() {
		chessBoard = new ChessBoard(X_GRID_NUMBER, Y_GRID_NUMBER);
		invalidate();
	}
}
